package com.jr.controller;

/**
 * @Function：
 * @Author 梁明哲
 * @Email 424577090@qq.com
 * @Package ${PACKAGE_NAME}
 * @Project invoice
 * @Date 2023-02-26 15:14:08
 */

import com.alibaba.fastjson.JSON;
import com.jr.entity.DetailView;
import com.jr.service.DetailViewService;
import com.jr.service.impl.DetailViewServiceImpl;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.dv")
@MultipartConfig
public class DetailViewController extends HttpServlet {
    private DetailViewService detailViewService = new DetailViewServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 3);
        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询相关信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void allDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {
            List<DetailView> allDetail = detailViewService.allDetail((Integer) request.getSession().getAttribute("enterpriseId"));
            out.write(JSON.toJSONString(new ResultUtil("allDetail",allDetail)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
